﻿/// <reference path="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" />
/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace("LO1200.Web");

LO1200.Web.Timer = function () {
    LO1200.Web.Timer.initializeBase(this);
};

LO1200.Web.Timer.prototype = {
    add_tick: function (fn) {
        this.get_events().addHandler("tick", fn);
        return this;
    },
    remove_tick: function (fn) {
        this.get_events().removeHandler("tick", fn);
        return this;
    },
    raiseTick: function () {
        var handler = this.get_events().getHandler("tick");
        if (handler) handler(this, Sys.EventArgs.Empty);
        if (this._running) this._timerID = setTimeout("$find('" + this.get_id() + "').raiseTick();", this.get_interval());
        return this;
    },
    get_interval: function () {
        if (!this._interval) return 1000;
        return this._interval;
    },
    set_interval: function (value) {
        if (value !== this._interval) {
            this._interval = value;
            this.raisePropertyChanged("interval");
        }
        return this;
    },
    start: function () {
        if (!this._running) {
            this._running = true;
            this._timerID = setTimeout("$find('" + this.get_id() + "').raiseTick();", this.get_interval());
        }
        return this;
    },
    stop: function () {
        if (this._running && this._timerID) {
            clearTimeout(this._timerID);
            delete this._timerID;
            this._running = false;
        }
        return this;
    },
    initialize: function () {
        LO1200.Web.Timer.callBaseMethod(this, 'initialize');
    },
    dispose: function () {
        this.stop();
        LO1200.Web.Timer.callBaseMethod(this, 'dispose');
    }
};

LO1200.Web.Timer.registerClass("LO1200.Web.Timer", Sys.Component);
LO1200.Web.GlobalTimer = $create(LO1200.Web.Timer, { id: "LO1200GlobalTimer", interval: 1000 }, null, null, null);

Sys.Application.add_init(function () {
    LO1200.Web.GlobalTimer.start(); // TODO: start on page load, not before.
});